#include <LDF/VisComponent.h>
#include <stdlib.h>
#include "DynamicImageStrategy.h" 
#include "ImageLoading.h"

DynamicImageStrategy::DynamicImageStrategy()
{
	imageLoader = new ImageLoading();
	counter = 0;
	random = 0;
}

DynamicImageStrategy::~DynamicImageStrategy()
{
	delete imageLoader;
}

void DynamicImageStrategy::addFile(std::string fileName)
{
	std::pair<std::string, unsigned int> element;
	element.first = fileName;
	element.second = (unsigned int) imageLoader->loadImageForTexture(fileName.c_str());

	imagesFiles.push_back(element);
}

void DynamicImageStrategy::draw(const std::vector<unsigned long>& selectedIds)
{
	float w2 = component->getWidth() / 2.0f;
	float h2 = component->getHeight() / 2.0f;

	int length = imagesFiles.size();
	//int random = 0;
	counter++;
	if(counter>100)
	{	
		random = rand()%length;
		counter = 0;
	}
	
	unsigned int imageTex = imagesFiles[random].second;

	glEnable(GL_TEXTURE_2D);	
		glColor3f(1.0, 1.0, 1.0);
		glBindTexture(GL_TEXTURE_2D, imageTex);
		glBegin(GL_QUADS);
			glTexCoord2f(0,0);
			glVertex3f(-w2,-h2,0);
			glTexCoord2f(1,0);
			glVertex3f(w2,-h2,0);
			glTexCoord2f(1,1);
			glVertex3f(w2,h2,0);
			glTexCoord2f(0,1);
			glVertex3f(-w2, h2,0);
		glEnd();
	glDisable(GL_TEXTURE_2D);
	glFlush();
	
}

void DynamicImageStrategy::drawForPicking()
{
	float w2 = component->getWidth() / 2.0f;
	float h2 = component->getHeight() / 2.0f;

	// Starting at the lower left corner and going counter-clockwise
	float v0[3], v1[3], v2[3], v3[3];
	v0[0] = -w2; v0[1] = -h2; v0[2] = 0;
	v1[0] = w2; v1[1] = -h2; v1[2] = 0;
	v2[0] = w2; v2[1] = h2; v2[2] = 0;
	v3[0] = -w2; v3[1] = h2; v3[2] = 0;
	
	unsigned char pickingColor[3] = { 0, 0, 0 };
	component->getIdColor(pickingColor);
	glColor3ubv(pickingColor);
	glBegin(GL_QUAD_STRIP);
		glVertex3fv(v0);
		glVertex3fv(v1);
		glVertex3fv(v3);
		glVertex3fv(v2);
	glEnd();
}

